package com.seed.web.servlet.sysmanager;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;

import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.seed.commons.dbutil.DBBean;
import com.seed.service.sysmanager.IMenuItem;
import com.seed.service.sysmanager.IMenuModel;
import com.seed.web.form.sysmanager.MenuItemForm;

/**
 * 系统菜单，返回JSON格式
 * @author yanghaoquan
 *
 */
public class MenuServlet extends HttpServlet{

	private static final long serialVersionUID = 1L;
	
	private WebApplicationContext ctx;

	@Override
	public void init() throws ServletException {
		ServletContext servletContext = this.getServletContext();
		this.ctx = WebApplicationContextUtils.getWebApplicationContext(servletContext);
	}
	
	
	
	/**
	 * 初始化系统菜单模块
	 */
	public void initMenuModel() {
		IMenuModel imm = (IMenuModel) this.ctx.getBean("menuModelService") ;
		
		List<DBBean> dblist = imm.queryMenuModel() ;
		
		this.getServletContext().setAttribute("MenuModelJson", dblist) ;
		
		initMenuItem(dblist) ;
	}
	
	/**
	 * 初始化系统菜单列表
	 */
	public void initMenuItem(List<DBBean> dblist) {
		
		Map<String,List<MenuItemForm>> menuMap = new HashMap<String,List<MenuItemForm>>() ;
		
		IMenuItem imi = (IMenuItem) this.ctx.getBean("menuItemService") ;
		
		for(DBBean db : dblist) {
			String mmid = db.getbean("MMID") ;
			
			List<MenuItemForm> menuItemList = new ArrayList<MenuItemForm>() ;
			
			//加载一级菜单列表
			Map<String,Object[]> mapKeyRoot = new HashMap<String,Object[]>() ;
			mapKeyRoot.put("BYMenuItemRoot", new Object[]{mmid,"Y"}) ;
			List<DBBean> dblist1 = imi.queryMenuItem(mapKeyRoot) ;
			for(DBBean db1 : dblist1) {
				
				//System.out.println(db1.getbean("mlid") +"\t"+ db1.getbean("MLName") +"\t"+ db1.getbean("MLISROOT") +"\t"+ db1.getbean("MLUrl") +"\t"+ db1.getbean("MLDesc") +"\t"+ db1.getbean("MLIcon") +"\t"+ db1.getbean("addDate") +"\t"+ db1.getbean("SEED_MENU_MODEL_MMID_FK") +"\t"+ db1.getbean("SEED_MENU_ITEM_MLID_FK"));
				
				MenuItemForm mroot = new MenuItemForm() ;
				mroot.setMlId(db1.getbean("mlid").toString()) ;
				mroot.setMlName(db1.getbean("MLName").toString()) ;
				mroot.setMlIsRoot(db1.getbean("MLISROOT").toString()) ;
				mroot.setMlUrl((String) db1.getbean("MLUrl")) ;
				mroot.setMlDesc(db1.getbean("MLDesc").toString()) ;
				mroot.setMlIcon((String)db1.getbean("MLIcon")) ;
				mroot.setAddDate(db1.getbean("addDate").toString()) ;
				mroot.setMenuItemFk((String)db1.getbean("SEED_MENU_ITEM_MLId_FK")) ;
				mroot.setMenuModelFK((String)db1.getbean("SEED_MENU_MODEL_MMId_FK")) ;
				
				menuItemList.add(mroot) ;
				
				//加载二级菜单列表
				Map<String,Object[]> mapKeyChild = new HashMap<String,Object[]>() ;
				mapKeyChild.put("BYMenuItemFK", new Object[]{db1.getbean("mlid")}) ;
				List<DBBean> dbchild = imi.queryMenuItem(mapKeyChild) ;
				for(DBBean db2 : dbchild) {
					//System.out.println(db2.getbean("mlid") +"\t"+ db2.getbean("MLName") +"\t"+ db2.getbean("MLISROOT") +"\t"+ db2.getbean("SEED_MENU_MODEL_MMID_FK") +"\t"+ db2.getbean("SEED_MENU_ITEM_MLID_FK"));
					
					MenuItemForm mchild = new MenuItemForm() ;
					mchild.setMlId(db2.getbean("mlid").toString()) ;
					mchild.setMlName(db2.getbean("MLName").toString()) ;
					mchild.setMlIsRoot(db2.getbean("MLISROOT").toString()) ;
					mchild.setMlUrl((String)db2.getbean("MLUrl")) ;
					mchild.setMlDesc(db2.getbean("MLDesc").toString()) ;
					mchild.setMlIcon((String)db2.getbean("MLIcon")) ;
					mchild.setMlFunbar((String)db2.getbean("MLFunbar")) ;
					mchild.setAddDate(db2.getbean("addDate").toString()) ;
					mchild.setMenuItemFk((String)db2.getbean("SEED_MENU_ITEM_MLId_FK")) ;
					mchild.setMenuModelFK((String)db2.getbean("SEED_MENU_MODEL_MMId_FK")) ;
					
					menuItemList.add(mchild) ;
				}
			}
			menuMap.put(mmid, menuItemList) ;
		}
		this.getServletContext().setAttribute("menulist", menuMap) ;
	}
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		this.doPost(req, resp) ;
		//initMenuModel() ;
	}
	
	@SuppressWarnings("unchecked")
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		
		String menumodel = req.getParameter("menumodel") ;
		String menuitem = req.getParameter("menuitem") ;
		
		//菜单模块
		if (null != menumodel && !"".equals(menumodel)) {
			List<DBBean> dblist = (List<DBBean>) this.getServletContext().getAttribute("MenuModelJson") ;
			
			JSONArray menuJSON = JSONArray.fromObject(dblist) ;
			resp.getWriter().println(menuJSON);
			return ;
		}
		
		//菜单列表
		if (null != menuitem && !"".equals(menuitem)) {
			Map<String,Object> map = (Map<String, Object>) this.getServletContext().getAttribute("menulist") ;
			
			JSONArray menuJSON = JSONArray.fromObject(map.get(menuitem)) ;
			resp.getWriter().println(menuJSON);
			
			return ;
		}
		/*
		Map<String,List<MenuItem>> menuMap = new HashMap<String,List<MenuItem>>() ;
		
		IMenuModel imm = (IMenuModel) this.ctx.getBean("menuModelService") ;
		List<DBBean> dblist = imm.queryMenuModel() ;
		
		IMenuItem imi = (IMenuItem) this.ctx.getBean("menuItemService") ;
		
		
		for(DBBean db : dblist) {
			String mmid = db.getbean("MMID") ;
			
			List<MenuItem> menuItemList = new ArrayList<MenuItem>() ;
			
			Map<String,Object[]> mapKeyRoot = new HashMap<String,Object[]>() ;
			mapKeyRoot.put("BYMenuItemRoot", new Object[]{mmid,"Y"}) ;
			List<DBBean> dblist1 = imi.queryMenuItem(mapKeyRoot) ;
			for(DBBean db1 : dblist1) {
				System.out.println(db1.getbean("mlid") +"\t"+ db1.getbean("MLName") +"\t"+ db1.getbean("MLISROOT") +"\t"+ db1.getbean("MLUrl") +"\t"+ db1.getbean("MLDesc") +"\t"+ db1.getbean("MLIcon") +"\t"+ db1.getbean("addDate") +"\t"+ db1.getbean("SEED_MENU_MODEL_MMID_FK") +"\t"+ db1.getbean("SEED_MENU_ITEM_MLID_FK"));
				
				MenuItem mroot = new MenuItem() ;
				mroot.setMlId(db1.getbean("mlid").toString()) ;
				mroot.setMlName(db1.getbean("MLName").toString()) ;
				mroot.setMlIsRoot(db1.getbean("MLISROOT").toString()) ;
				mroot.setMlUrl((String) db1.getbean("MLUrl")) ;
				mroot.setMlDesc(db1.getbean("MLDesc").toString()) ;
				mroot.setMlIcon((String)db1.getbean("MLIcon")) ;
				mroot.setAddDate(db1.getbean("addDate").toString()) ;
				
				menuItemList.add(mroot) ;
				
				Map<String,Object[]> mapKeyChild = new HashMap<String,Object[]>() ;
				mapKeyChild.put("BYMenuItemFK", new Object[]{db1.getbean("mlid")}) ;
				
				List<DBBean> dbchild = imi.queryMenuItem(mapKeyChild) ;
				for(DBBean db2 : dbchild) {
					System.out.println(db2.getbean("mlid") +"\t"+ db2.getbean("MLName") +"\t"+ db2.getbean("MLISROOT") +"\t"+ db2.getbean("SEED_MENU_MODEL_MMID_FK") +"\t"+ db2.getbean("SEED_MENU_ITEM_MLID_FK"));
					
					MenuItem mchild = new MenuItem() ;
					mchild.setMlId(db2.getbean("mlid").toString()) ;
					mchild.setMlName(db2.getbean("MLName").toString()) ;
					mchild.setMlIsRoot(db2.getbean("MLISROOT").toString()) ;
					mchild.setMlUrl((String)db2.getbean("MLUrl")) ;
					mchild.setMlDesc(db2.getbean("MLDesc").toString()) ;
					mchild.setMlIcon((String)db2.getbean("MLIcon")) ;
					mchild.setAddDate(db2.getbean("addDate").toString()) ;
					
					menuItemList.add(mroot) ;
				}
			}
			menuMap.put(mmid, menuItemList) ;
		}
		resp.getWriter().print(menuMap) ;*/
		//System.out.println(db1.getbean("mlid") +"\t"+ db1.getbean("MLName") +"\t"+ db1.getbean("MLISROOT") +"\t"+ db1.getbean("SEED_MENU_MODEL_MMID_FK") +"\t"+ db1.getbean("SEED_MENU_ITEM_MLID_FK"));
		/*for(DBBean db : dblist) {
			String mmid = db.getbean("MMID") ;
			
			for(DBBean db1 : dblist1) {
				String mmfk = db1.getbean("SEED_MENU_MODEL_MMID_FK") ;
				String mlid1 = "" ;
				if(mmid.equals(mmfk)) {
					mlid1 = db1.getbean("mlid") ;
					System.out.println(db1.getbean("mlid") +"\t"+ db1.getbean("MLName") +"\t"+ db1.getbean("MLISROOT") +"\t"+ db1.getbean("SEED_MENU_MODEL_MMID_FK") +"\t"+ db1.getbean("SEED_MENU_ITEM_MLID_FK"));
				}
				System.out.println(mlid1+"==="+db1.getbean("SEED_MENU_ITEM_MLID_FK"));
				if(mlid1.equals(db1.getbean("SEED_MENU_ITEM_MLID_FK"))) {
					System.out.println(db1.getbean("mlid") +"\t"+ db1.getbean("MLName") +"\t"+ db1.getbean("MLISROOT") +"\t"+ db1.getbean("SEED_MENU_MODEL_MMID_FK") +"\t"+ db1.getbean("SEED_MENU_ITEM_MLID_FK"));
				}
				
			}
		}*/
		//JSONArray menuJSON = JSONArray.fromObject(dblist) ;
		//resp.getWriter().println(menuJSON);
	}

}
